package com.tencent.mm.opensdk.openapi;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;

import com.tencent.mm.opensdk.b.c.a;
import com.tencent.mm.opensdk.b.c.b;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Map;
import java.util.Set;

class MMSharedPreferences implements SharedPreferences {
    class REditor implements SharedPreferences.Editor {
        private boolean clear;
        private ContentResolver cr;
        private Set remove;
        private Map values;

        public REditor(ContentResolver arg2) {

            this.values = new HashMap();
            this.remove = new HashSet();
            this.clear = false;
            this.cr = arg2;
        }

        public void apply() {
        }

        public SharedPreferences.Editor clear() {
            this.clear = true;
            return this;
        }

        public boolean commit() {
            int v1_1;
            String v5 = null;
            ContentValues v4 = new ContentValues();
            if (this.clear) {
                this.cr.delete(b.CONTENT_URI, v5, ((String[]) null));
                this.clear = false;
            }

            Iterator v1 = this.remove.iterator();
            while (v1.hasNext()) {
                this.cr.delete(b.CONTENT_URI, "key = ?", new String[]{(String) v1.next()});
            }

            Iterator v5_1 = this.values.entrySet().iterator();
            while (v5_1.hasNext()) {
                Object v0 = v5_1.next();
                Object v6 = ((Map.Entry) v0).getValue();
                if (v6 == null) {
                    Log.e("MicroMsg.SDK.PluginProvider.Resolver", "unresolve failed, null value");
                    v1_1 = 0;
                } else if ((v6 instanceof Integer)) {
                    v1_1 = 1;
                } else if ((v6 instanceof Long)) {
                    v1_1 = 2;
                } else if ((v6 instanceof String)) {
                    v1_1 = 3;
                } else if ((v6 instanceof Boolean)) {
                    v1_1 = 4;
                } else if ((v6 instanceof Float)) {
                    v1_1 = 5;
                } else if ((v6 instanceof Double)) {
                    v1_1 = 6;
                } else {
                    Log.e("MicroMsg.SDK.PluginProvider.Resolver", "unresolve failed, unknown type=" + v6.getClass().toString());
                    v1_1 = 0;
                }

                if (v1_1 == 0) {
                    v1_1 = 0;
                } else {
                    v4.put("type", Integer.valueOf(v1_1));
                    v4.put("value", v6.toString());
                    v1_1 = 1;
                }

                if (v1_1 == 0) {
                    continue;
                }
                this.cr.update(b.CONTENT_URI,
                        v4,
                        "key = ?",
                        new String[]{(String) ((Entry) v0).getKey()});
            }

            return true;
        }

        public SharedPreferences.Editor putBoolean(String arg3, boolean arg4) {
            this.values.put(arg3, Boolean.valueOf(arg4));
            this.remove.remove(arg3);
            return this;
        }

        public SharedPreferences.Editor putFloat(String arg3, float arg4) {
            this.values.put(arg3, Float.valueOf(arg4));
            this.remove.remove(arg3);
            return this;
        }

        public SharedPreferences.Editor putInt(String arg3, int arg4) {
            this.values.put(arg3, Integer.valueOf(arg4));
            this.remove.remove(arg3);
            return this;
        }

        public SharedPreferences.Editor putLong(String arg3, long arg4) {
            this.values.put(arg3, Long.valueOf(arg4));
            this.remove.remove(arg3);
            return this;
        }

        public SharedPreferences.Editor putString(String arg2, String arg3) {
            this.values.put(arg2, arg3);
            this.remove.remove(arg2);
            return this;
        }

        public SharedPreferences.Editor putStringSet(String arg2, Set arg3) {
            return null;
        }

        public SharedPreferences.Editor remove(String arg2) {
            this.remove.add(arg2);
            return this;
        }
    }

    private static final String TAG = "MicroMsg.SDK.SharedPreferences";
    private final String[] columns;
    private final ContentResolver cr;
    private REditor editor;
    private final HashMap values;

    public MMSharedPreferences(Context arg4) {

        this.columns = new String[]{"_id", "key", "type", "value"};
        this.values = new HashMap();
        this.editor = null;
        this.cr = arg4.getContentResolver();
    }

    public boolean contains(String arg2) {
        boolean v0 = this.getValue(arg2) != null ? true : false;
        return v0;
    }

    public SharedPreferences.Editor edit() {
        if (this.editor == null) {
            this.editor = new REditor(this.cr);
        }

        return this.editor;
    }

    public Map getAll() {
        HashMap v0_3;
        Map v0_2;
        Map v6 = null;
        try {
            Cursor v0_1 = this.cr.query(b.CONTENT_URI, this.columns, null, null, null);
            if (v0_1 == null) {
                v0_2 = v6;
                return v0_2;
            }

            int v1 = v0_1.getColumnIndex("key");
            int v2 = v0_1.getColumnIndex("type");
            int v3 = v0_1.getColumnIndex("value");
            while (v0_1.moveToNext()) {
                this.values.put(v0_1.getString(v1), a.a(v0_1.getInt(v2), v0_1.getString(v3)));
            }

            v0_1.close();
            v0_3 = this.values;
            return v0_3;
        } catch (Exception v0) {
            Log.e("MicroMsg.SDK.SharedPreferences", "getAll exception:" + v0.getMessage());
            v0_3 = this.values;
            return v0_3;
        }
    }

    public boolean getBoolean(String arg3, boolean arg4) {
        Object v0 = this.getValue(arg3);
        if (v0 != null && ((v0 instanceof Boolean))) {
            arg4 = ((Boolean) v0).booleanValue();
        }

        return arg4;
    }

    public float getFloat(String arg3, float arg4) {
        Object v0 = this.getValue(arg3);
        if (v0 != null && ((v0 instanceof Float))) {
            arg4 = ((Float) v0).floatValue();
        }

        return arg4;
    }

    public int getInt(String arg3, int arg4) {
        Object v0 = this.getValue(arg3);
        if (v0 != null && ((v0 instanceof Integer))) {
            arg4 = ((Integer) v0).intValue();
        }

        return arg4;
    }

    public long getLong(String arg3, long arg4) {
        Object v0 = this.getValue(arg3);
        if (v0 != null && ((v0 instanceof Long))) {
            arg4 = ((Long) v0).longValue();
        }

        return arg4;
    }

    public String getString(String arg3, String arg4) {
        Object v0 = this.getValue(arg3);
        if (v0 == null || !(v0 instanceof String)) {
            String v0_1 = arg4;
        }

        return ((String) v0);
    }

    public Set getStringSet(String arg2, Set arg3) {
        return null;
    }

    private Object getValue(String arg8) {
        Object v0_1;
        Cursor v1;
        Object v6 = null;
        try {
            v1 = this.cr.query(b.CONTENT_URI, this.columns, "key = ?", new String[]{arg8}, null);
            if (v1 == null) {
                v0_1 = v6;
                return v0_1;
            }

            int v0_2 = v1.getColumnIndex("type");
            int v2 = v1.getColumnIndex("value");
            if (v1.moveToFirst()) {
                v0_1 = a.a(v1.getInt(v0_2), v1.getString(v2));
            } else {
                v0_1 = v6;
            }
            v1.close();
        } catch (Exception v0) {
            Log.e("MicroMsg.SDK.SharedPreferences", "getValue exception:" + v0.getMessage());
            v0_1 = v6;
        }

        return v0_1;
    }

    public void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener arg1) {
    }

    public void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener arg1) {
    }
}

